package com.formen.ndk;


import java.util.List;
import java.util.Locale;

import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class GPSTest extends Activity implements LocationListener{

	TextView tv1 = null;	//�����浵ǥ��
	TextView tv2 = null;	//�ּ�ǥ��

	//��ġ���� ��ü
	LocationManager lm = null;
	//��ġ���� ��ġ �̸�
	String provider = null;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.p2);      

		tv1 = (TextView)findViewById(R.id.tvName);
		tv2 = (TextView)findViewById(R.id.tvMeet);

		//��ġ���� ��ü ��
		lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		
		//��ġ���� �ϵ���� ���
		Criteria c = new  Criteria();
		
		//������ �ϵ�����ϸ� ���Ϲ���
		provider = lm.getBestProvider(c, true);


		//���������� �޾Ҵ� GPS �� ���
		Location location = lm.getLastKnownLocation(provider);

		//location
		if(location ==null){
			//��ġ�� ������� ������ ��� ���â�� ���
			Toast.makeText(this, "��밡������ġ���������ڰ� ����ϴ�.", Toast.LENGTH_LONG).show();
		}else{
			onLocationChanged(location);
			
			// + �߰� :  �������� ����â���� �̵�
			Intent intent = new Intent();
			intent.setClassName( "com.android.settings", "com.android.settings.wifi.WifiSettings" );
			startActivity(intent);
		}

	}

	//�� ȭ���� �Ҹ��� �Ͻ����� ����
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		Location location = lm.getLastKnownLocation(provider);
		lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 0, this); //��Ʈ��ũ

	}

	//�ٸ�ȭ������ �Ѿ�� �Ͻ� ����
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();

		//��ġ���� ��ü�� �̺�Ʈ ����
		lm.removeUpdates(this);
	}


	//��ġ�� ���Ҷ� ���� ȣ�� 
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub

		//����, �浵
		double lat = location.getLatitude();
		double lng = location.getLongitude();

		tv1.setText("���� : "+ String.valueOf(lat)+ ", �浵 : " +String.valueOf(lng));
		tv2.setText(getAddress(lat, lng));	//�ּҹ�ȯ�޼ҵ� ȣ��


	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	//������ �浵�� �ּҰ��� ��ȯ�ϴ� �޼ҵ�
	public String getAddress(double lat, double lng){
		String address =  null;

		//��ġ������ Ȱ���ϱ� ���� ���� API��ü
		Geocoder geocoder = new Geocoder(this, Locale.getDefault());

		//�ּҸ���� ���� HashMap
		List<Address> list = null;


		try {
			list = geocoder.getFromLocation(lat, lng, 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// ��ȿ���˻�
		if(list == null){
			//������
			Log.e("getAddress","�ҷ����� ����");
			return null;
		}

		// 주소 받아오기
		if(list.size()>0){
			Address addr = list.get(0);

			address = addr.getCountryName() + " "
					+ addr.getLocality() + " "
					+ addr.getThoroughfare()+ " "
					+ addr.getFeatureName();
		}

		return address;	

	}







}